package com.xiaolanshu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaolanshu.entity.Dataurl;
import com.xiaolanshu.mapper.DataurlMapper;
import com.xiaolanshu.service.DataurlService;
import com.xiaolanshu.template.OssTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.List;

@Service
public class DataurlServiceImpl implements DataurlService {

    @Autowired
    private DataurlMapper dataurlMapper;
    @Autowired
    private OssTemplate ossTemplate;
    //获取笔记图片列表
    @Override
    public List<String> findPictureListById(Long noteId) {
        //根据笔记id获取图片列表
        LambdaQueryWrapper<Dataurl> lqw = new LambdaQueryWrapper<>();
        //根据笔记id查询，仅查询pictureurl字段且不为空
        lqw.select(Dataurl::getPictureurl).isNotNull(Dataurl::getPictureurl).eq(Dataurl::getNoteId,noteId);
        List<Dataurl> dataurls = dataurlMapper.selectList(lqw);
        //当查询出的图片列表不为空时，遍历取出
        if (dataurls!=null){
            //遍历获取图片
            List<String> pictureurlList = new ArrayList<>();
            for (Dataurl d: dataurls
            ) {
                pictureurlList.add(d.getPictureurl());
            }
            return pictureurlList;
        }
        return null;
    }
    //获取笔记视频
    @Override
    public Dataurl findVideo(Long noteId) {
        LambdaQueryWrapper<Dataurl> lqw = new LambdaQueryWrapper<>();
        //根据笔记id查询，仅查询videourl字段且不为空
        lqw.select(Dataurl::getVideourl).isNotNull(Dataurl::getVideourl).eq(Dataurl::getNoteId,noteId);
        return dataurlMapper.selectOne(lqw);
    }
    //根据笔记id保存视频信息
    @Override
    public void saveVideo(Dataurl dataurlVideo) {
        dataurlMapper.insert(dataurlVideo);
    }
    //根据笔记id保存图片信息
    @Override
    public void savePicture(Dataurl dataurlPicture) {
        dataurlMapper.insert(dataurlPicture);
    }
    //上传笔记视频
    @Override
    public String uploadVideo(MultipartFile video) throws Exception {
        //将视频上传到阿里云Oss
        String videoUrl = ossTemplate.upload(video.getOriginalFilename(), video.getInputStream());
        return videoUrl;
    }
    //上传笔记视频
    @Override
    public String uploadPicture(MultipartFile picture) throws Exception {
        //将图片上传到阿里云Oss
        String pictureUrl = ossTemplate.upload(picture.getOriginalFilename(), picture.getInputStream());
        return pictureUrl;
    }

}
